<template>
	<view class='cu-modal modal-bottom' :class='{show: show}'>
		<view class='modal'>
			<view class='head'>
				<text class='title'>添加客户</text>
				<text class='close cuIcon-close' @click='handleClose'></text>
			</view>
			<view class='form'>
				<view class='form-item'>
					<view class='label'>手机号</view>
					<view class='input'>
						<input v-model="form.telphone" type="number" placeholder="输入客户手机号" maxlength="11">
					</view>
				</view>
				<view class='form-item'>
					<view class='label'>客户姓名</view>
					<view class='input'>
						<input v-model="form.name" type="text" placeholder="输入客户姓名" maxlength='10'>
					</view>
				</view>
				<view class='form-item'>
					<view class='label'>标签</view>
					<view class='tags'>
						<view class='tag-item' v-if="item.id" :class='{active: selects.includes(item.id)}' v-for='item in tags'
							:key='item.id' @click='handleSelect(item)'>{{item.title}}</view>
					</view>
				</view>
			</view>
			<view class='submit' @click='handleSubmit'>确定</view>
		</view>
	</view>
</template>

<script>
	import regRule from '@/common/rules'
	export default {
		data() {
			return {
				show: false,
				selects: [],
				tags: [],
				success:null,
				form:{},
			}
		},
		created() {
			
		},
		methods: {
			handleOpen(e) {
				this.selects = []
				this.tags = JSON.parse(JSON.stringify(e.tags)) 
				this.success = e.success
				this.form = {
					name:'',
					telphone:'',
				}
				this.show = true;
			},
			handleClose() {
				this.show = false;
			},
			handleSubmit() {
				if(!this.form.name) return uni.showToast({title:'请填写名称',icon:'none'})
				if(!this.form.telphone) return uni.showToast({title:'请填写手机号',icon:'none'})
				if (!regRule.phone.test(this.form.telphone)) return uni.showToast({icon: 'none',title: '请输入正确的手机号'})
				if(this.success){
					this.success({...this.form,labels:this.selects.length?this.selects.join(','):''})
				}
				this.handleClose()
			},
			handleSelect(e) {
				if(this.selects.includes(e.id)) {
					let index = this.selects.findIndex(item => item == e.id);
					this.selects.splice(index,1)
				} else {
					this.selects.push(e.id)
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.modal {
		width: 100%;
		position: absolute;
		left: 0;
		bottom: 0;
		background: #FFFFFF;
		padding: 0 32rpx 76rpx 32rpx;
		text-align: left;
		border-radius: 40rpx 40rpx 0rpx 0rpx;
		
		.head {
			text-align: center;
			font-weight: 500;
			font-size: 30rpx;
			color: var(--h1Color);
			padding: 32rpx 0 64rpx 0;
			position: relative;
			
			.close {
				position: absolute;
				top:  32rpx;
				right: 0;
			}
		}
		
		.form {
			.form-item {
				margin-bottom: 64rpx;
				
				&:last-child {
					margin: 0;
				}
				
				.label {
					font-weight: 600;
					font-size: 30rpx;
					color: var(--h1Color);
					line-height: 42rpx;
					margin-bottom: 24rpx;
				}
				
				.input {
					width: 100%;
					height: 90rpx;
					background: #F8F8F8;
					border-radius: 8rpx;
					padding: 0 28rpx;
					
					input {
						width: 100%;
						height: 100%;
					}
				}
				
				.tags {
					display: flex;
					flex-wrap: wrap;
					gap: 16rpx;
					
					.tag-item {
						height: 72rpx;
						background: var(--tagBg);
						border-radius: 8rpx;
						padding: 0 40rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						font-size: 26rpx;
						color: var(--h1Color);
						
						&.active {
							background: var(--primaryBg);
							border: 2rpx solid #FFE359;
						}
					}
				}
			}
		}
		
		.submit {
			margin-top: 72rpx;
			width: 100%;
			height: 96rpx;
			background: var(--primaryGradBg);
			border-radius: 48rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-weight: 600;
			font-size: 32rpx;
			color: var(--btnColor);
		}
	}
</style>